package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Invmd;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;

@Repository("invmdDao")
public class InvmdDaoImpl implements InvmdDao {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public PagesBean getInvmdList(String maaa, String maab, int offset,int pageSize) throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Invmd> list = new ArrayList<Invmd>();
		StringBuffer sql = new StringBuffer("SELECT * FROM purmb ");
		sql.append( " where mbaa = '" + Function.formatSQLString(maaa) + "'");
		sql.append( " and mbab = '" + Function.formatSQLString(maab) + "'");
		sql.append(" ORDER BY mbac ASC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Invmd>(Invmd.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int getMaxMdae()throws DatabaseException {
		int mbac = namedParameterJdbcTemplate.getJdbcOperations().queryForInt("select IFNULL(max(mbac),0) mbac from invmd ");
		if(mbac==0){
			mbac = 10000;
		}else{
			mbac += 10000;
		}
		return mbac;
	}
	@Override
	public int saveInvmd(Invmd invmd) throws DatabaseException {
		String sql = "INSERT INTO invmd (mbaa,mbab,mbac,mbad,mbae,mbaf,mbag,mbah,mbai,mbaj,mbak,mbal) VALUES (:mbaa,:mbab,:mbac,:mbad,:mbae,:mbaf,:mbag,:mbah,:mbai,:mbaj,:mbak,:mbal)";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(invmd);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int delInvmd(String mbaa,String mbab,String mbacs) throws DatabaseException {
		String sql = "DELETE FROM invmd WHERE mbaa=? and mbab=? and mbac in ("+mbacs+")";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{mbaa,mbab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int updateInvmd(Invmd invmd) throws DatabaseException {
		String sql = "UPDATE invmd SET mbad=:mbad, mbae=:mbae,mbaf=:mbaf,mbag=:mbag,mbah=:mbah,mbai=:mbai,mbaj=:mbaj, mbak=:mbak,mbal=:mbal WHERE mbaa=:mbaa and mbab = :mbab and mbac =:mbac";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(invmd);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int changeStatus(String mbaa, String mbab, String status)throws DatabaseException {
		String sql = "UPDATE purma SET maac=? WHERE maaa=? and maab = ?";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{status,mbaa,mbab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public void delInvmdByAaAb(String maaa, String maab) throws DatabaseException {

		String sql2 = "delete from purmb where mbaa = ? and mbab = ?";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql2, new Object[]{maaa,maab});
		
		
	}
}
